<div id="main">
	<div id="spacing">
		<div id="info">
			<div class="left">
				<div class="table">
					<h3>Information</h3>
					<ul>
						<li>
						  <%# Display all identifiers for an item %>
						  <% if @item.identifiers.empty? %>
						    <h5>Missing Identifier</h5>
						  <% else %>
                <h5>Identifier &mdash; <%= link_to 'print', :controller => "items/barcode", :action => @item.id %></h5>
  						  <% @item.identifiers.each do |id| %>
    							<p><%= id.identifier_value %></p>
    							<%= show_barcode(id.identifier_value) %>
  							<% end %>
							<% end %>
						</li>
						<% if @bag_link_text %>
							<li>
								<h5>Contained Within</h5>
								<p><%= link_to @bag_link_text, item_path(@container), :target => "_blank" %></p>
							</li>
						<% end %>
						<li>
							<h5>From</h5>
							<% @source = @item.source %>
							<p><%= (@source)?(@source.name):("No source set") %></p>
						</li>
						<li>
							<h5>To</h5>
							<% @destination = @item.destination %>
							<p><%= (@destination)?(@destination.name):("No destination set") %></p>
						</li>
						<li>
							<h5>Attributes</h5>
							<% @item.item_attributes.each do |attribute| %>
								<% @attribute_set = attribute.item_attribute_set %>
								<% if (@attribute_set) %>
									<h5><%= @attribute_set.name %></h5>
								<% else %>
									<h5>Other Attributes</h5>
								<% end %>
								<p class="tags">
									<em style="color:<%=h attribute.display_color %>;"><%= attribute.display_text %></em>
								</p>							
							<% end %>
						</li>
					</ul>
				</div>
			</div>
			<div class="right">
				<div class="history">
					<h3>Status</h3>
					<% checkins = CheckIn.find_all_by_item_id(@item.id, :order => "created_at DESC") %>
					<% @status = get_status(@item, checkins) %>
					<% if @item.lost_item %>
						<p>Lost<%= " (Resolved)" if @item.lost_item.resolved %></p>
						<% if @item.lost_item.comment != "" %>
							<p><%= @item.lost_item.comment %></p>
						<% end %>
					<% else %>
						<p><%= @status %></p>
					<% end %>
					<h3>History</h3>
					<ul>
						<% checkins.each do |checkin| %>
							<li>
								<h5><%= checkin.updated_at.in_time_zone('Pacific Time (US & Canada)').strftime("%m/%d/%Y at %I:%M%p") %></h5>
								<p>
									<% @action = case checkin.action 
													when "initial_checkin" then "added"
													when "pick_up" then 		"picked up"
													when "drop_off" then 		"checked in"
													else "Unknown Action"
											end
												%>
									<% @preposition = case checkin.action 
														when "initial_checkin" 	then 	"to system at"
														when "pick_up" 			then 	"at"
														when "drop_off" 		then 	"at"
														else "at"
												end
													%>
										<%= checkin.user.real_name rescue "Unknown User" %> 
										<%= @action %> 
										item 
										<%= @preposition %> 
										<%= checkin.location.name rescue "Unknown Location" %>.
									</p>
								</li>
							<% end %>
					</ul>
				</div>
				<div class="table">
					<h3>Comments</h3>
					<% @comments = @item.comments %>
					<ul>
						<% if (@comments.first) then %>
							<% @comments.each do |comment| %>
								<li>
									<h5>
										<%= link_to comment.user.real_name, user_path(comment.user.id) rescue "Unknown User" %>
										<%= begin " at " + comment.user.location.name rescue "" end %>
										&mdash; 
										<span><%= comment.updated_at.in_time_zone('Pacific Time (US & Canada)').strftime("%m/%d/%Y at %I:%M%p") %></span>
									</h5>
									<p><%= comment.text %></p>
								</li>
							<% end %>
						<% else %>
							<li>
								<h5>No Comments Yet</h5>
								<p>Add a comment below.</p>
							</li>
						<% end %>
						<li>
							<h5>You</h5>
							<% form_for @item do |f| %>
								<p>
									<%= hidden_field_tag "comment[user_id]", @current_user.id %>
									<%= hidden_field_tag "comment[item_id]", @item.id %>
									<%= text_area :comment, :text %>
									<%= f.submit 'Comment' %>
									<%= hidden_field_tag :from, request.url %>
								</p>
							<% end %>
						</li>
					</ul>
				</div>
			</div>
		</div>
		<% if (@type && @type.is_container) then %>
			<div id="list-header">
				<div class="left">
					<% @items_inside = Item.find_all_by_contained_by_id(@item.id) %>
					<h3>
						Inside This <%= @type.item_type %>
					</h3>
				</div>
				<div class="options">
					<h4 class="left">
						<span>
							<% @contained_colors = @items_inside.map {
									|i| i.item_attributes.map {|a|
											{	:display_text => a.display_text,
												:display_color => a.display_color
											} if a.item_attribute_set and a.item_attribute_set.name == "Color"
									}
								}.flatten %>
							<% @contained_colors_final = @contained_colors.group_by {|c| c} %>
							<% @contained_colors_final.each do |c| %>
								<% if c[0] %>
									<% @show_colors = true %>
								<% end %>
							<% end %>
							<% if @show_colors %>
								<% @contained_colors_final.each do |c| %>
									<% if c[0] %>
										<em style="color:<%=h c[1][0][:display_color] %>">
											<%= c[1].count %>
											<%= c[1][0][:display_text] %>&nbsp;
										</em>
									<% end %>
								<% end %>
							<% end %>
						</span>
					<h4>
					<% if not @current_user.permission_level == 'courier' %>
						<ul class="right">
							<%= link_to "Add to " + @item_type, :action => 'add_to_container', :id => @item.id %>
						</ul>
					<% end %>
				</div>
			</div>
			<div id="list">
				<div class="header">
					<p class="first">Items</p>
					<p class="third">Attributes</p>
					<p class="fourth"><%= @items_inside.count %> <%= (@items_inside.count == 1)?("Item"):("Items") %></p>
				</div>
				<ul>
					<% if @items_inside.any? %>
						<% @items_inside.each do |i| %>
							<li onclick="document.location.href='<%= item_path(i) %>'" onhover="">
								<a name="<%= i.id %>">
									<span class="first" style="padding-top:7px">
										<% # Need to change this when start requiring item types %>
										<%= (i.item_type)?(i.item_type.item_type):("No Type") %>: 
										<%= i.identifiers.first.identifier_value %>
									</span>
									<span class="third" style="padding-top:7px">
										<% i.item_attributes.each do |attribute| %>
											<em style="color:<%=h attribute.display_color %>;"><%= attribute.display_text %></em>
										<% end %>
									</span>
									<span class="fourth">
										<% if not @current_user.permission_level == 'courier' %>
											<% form_for i do |f| %>
												<%= f.submit "Remove", :confirm => 'Are you sure?' %>
											<% end %>
										<% end %>
									</span>
								</a>
							</li>
						<% end %>
					<% else %>
						<li>
							<a name="noItems"><span class="first">No Items</span></a>
						</li>
					<% end %>
				</ul>
			</div>
		<% end %>
	</div>
</div>
